package com.zeny.coding.controller.admin.system;


import com.zeny.coding.constants.CourseConstants;
import com.zeny.coding.dto.PageDto;
import com.zeny.coding.dto.ResourceDto;
import com.zeny.coding.dto.Response;
import com.zeny.coding.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <h2>资源管理</h2>
 *
 * @Author Zeny
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/system/admin/resource")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    @PostMapping("/list")
    public Response list(@RequestBody PageDto<ResourceDto> pageDto) {
        pageDto.check();
        resourceService.list(pageDto);
        return new Response().success().data(pageDto).message(CourseConstants.QUERY_SUCCESS);
    }

    @PostMapping("/save")
    public Response save(@RequestBody ResourceDto resourceDto) {
        resourceDto.isValidForSave();
        this.resourceService.save(resourceDto);
        return new Response().success().message(CourseConstants.SAVE_SUCCESS);
    }

    @PutMapping("/update")
    public Response update(@RequestBody ResourceDto resourceDto) {
        resourceDto.isValidForUpdate();
        this.resourceService.update(resourceDto);
        return new Response().success().message(CourseConstants.UPDATE_SUCCESS);
    }

    @GetMapping("/findById/{id}")
    public Response findById(@PathVariable("id") String id) {
        if (StringUtils.isEmpty(id)) {
            return new Response().fail().message(CourseConstants.REQUEST_PARAMS_ERROR);
        }
        ResourceDto resourceDto = this.resourceService.findById(id);
        return new Response()
                .success()
                .data(resourceDto)
                .message(CourseConstants.QUERY_SUCCESS);
    }

    @DeleteMapping("/deleteById/{id}")
    public Response deleteById(@PathVariable("id") String id) {
        if (StringUtils.isEmpty(id)) {
            return new Response().fail().message(CourseConstants.REQUEST_PARAMS_ERROR);
        }
        this.resourceService.deleteById(id);
        return new Response().success().message(CourseConstants.DELETE_SUCCESS);
    }

    /**
     * <h2>获取树形资源列表的数据</h2>
     * @param id
     **/
    @GetMapping("/findTreeResources")
    public Response findTreeResources() {
        List<ResourceDto> treeResources = this.resourceService.findTreeResources();
        return new Response().success().data(treeResources).message(CourseConstants.QUERY_SUCCESS);
    }

    /**
     * <h2>根据角色id查询资源</h2>
     * @param id
     **/
    @GetMapping("/findResourcesByRoleId/{roleId}")
    public Response findResourcesByRoleId(@PathVariable("roleId") String roleId) {
        List<String> resourceIds = this.resourceService.findResourcesByRoleId(roleId);
        return new Response().success().data(resourceIds).message(CourseConstants.QUERY_SUCCESS);
    }

    /**
     * <h2>查询所有的资源</h2>
     * @param
     **/
    @GetMapping("/all")
    public Response all(){
        List<ResourceDto> resourceDtos = this.resourceService.findAll();
        return new Response().success().data(resourceDtos);
    }

}
